package com.sms.biz.impl;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;

import com.sms.biz.IWhiteListBiz;
import com.sms.dao.IWhiteListDAO;
import com.sms.po.Whitelist;


/**
 * @author 20111009 by cxh
 */
public class WhiteListBizImpl implements IWhiteListBiz {
	private IWhiteListDAO ibld = null;
	public void setIbld(IWhiteListDAO ibld){ this.ibld = ibld; }
	@Override
	public List<Whitelist> showAllWhiteList() {
		return ibld.showAllWhiteList();
	}

	@Override
	public List<Whitelist> showAllWhiteListByPage(String userId, String phone, int start, int limit) {
		// 离线查询
		DetachedCriteria criteria = DetachedCriteria
				.forClass(Whitelist.class);
		// 如果条件标题不为空,那么将条件标题附加上
		if (!"".equals(userId) && null != userId) {
			criteria.add(Restrictions.like("userId", userId,
					MatchMode.ANYWHERE));
		}
		if (!"".equals(phone) && null != phone) {
			criteria.add(Restrictions.like("phone", phone,
					MatchMode.ANYWHERE));
		}
		if (start <= 0) {start = 0;		}
		if (limit <= 0) {limit = 10;	}
		return ibld.showAllWhiteListByPage(criteria, start, limit);
	}
	@Override
	public int getAllWhiteListCount(String userId,String phone) {
		// 离线查询
		DetachedCriteria criteria = DetachedCriteria
				.forClass(Whitelist.class);
		// 如果条件标题不为空,那么将条件标题附加上
		if (!"".equals(userId) && null != userId) {
			criteria.add(Restrictions.like("userId", userId,
					MatchMode.ANYWHERE));
		}
		if (!"".equals(phone) && null != phone) {
			criteria.add(Restrictions.like("phone", phone,
					MatchMode.ANYWHERE));
		}
		return ibld.getAllWhiteListCount(criteria);
	}
	@Override
	public boolean addWhiteList(Whitelist bl) {
		try {
			ibld.addWhiteList(bl);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}
	@Override
	public boolean removeWhiteList(Whitelist bl) {
//		return ibld.removeWhiteList(bl);
		try {
			ibld.removeWhiteList(bl);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}
	@Override
	public boolean updateWhiteList(Whitelist bl) {
//		return ibld.updateWhiteList(bl);
		try {
			ibld.updateWhiteList(bl);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

}
